package com.ruoyi.standingbook.cleaning.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.asset.homepage.constant.StatusConstant;
import com.ruoyi.asset.homepage.enums.ServiceTypeEnum;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.standingbook.cleaning.domain.Cleaning;
import com.ruoyi.standingbook.cleaning.domain.bo.CleaningBo;
import com.ruoyi.standingbook.cleaning.domain.bo.CleaningPicBo;
import com.ruoyi.standingbook.cleaning.domain.vo.CleaningImportDetailVo;
import com.ruoyi.standingbook.cleaning.domain.vo.CleaningPicVo;
import com.ruoyi.standingbook.cleaning.domain.vo.CleaningVo;
import com.ruoyi.standingbook.cleaning.mapper.CleaningMapper;
import com.ruoyi.standingbook.cleaning.service.ICleaningService;
import com.ruoyi.system.service.ISysDictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 清扫保洁Service业务层处理
 *
 * @author cc
 * @date 2023-04-26
 */
@RequiredArgsConstructor
@Service
public class CleaningServiceImpl implements ICleaningService {

    private final CleaningMapper baseMapper;
    private final ISysDictDataService sysDictDataService;

    /**
     * 查询清扫保洁
     */
    @Override
    public CleaningVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询清扫保洁列表
     */
    @Override
    public TableDataInfo<CleaningVo> queryPageList(CleaningBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Cleaning> lqw = buildQueryWrapper(bo);
        Page<CleaningVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询清扫保洁列表
     */
    @Override
    public List<CleaningVo> queryList(CleaningBo bo) {
        LambdaQueryWrapper<Cleaning> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Cleaning> buildQueryWrapper(CleaningBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Cleaning> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getItemName()), Cleaning::getItemName, bo.getItemName());
        lqw.eq(bo.getServiceType() != null, Cleaning::getServiceType, bo.getServiceType());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), Cleaning::getLevel, bo.getLevel());
        lqw.between(params.get("beginDate") != null && params.get("endDate") != null,
            Cleaning::getDate ,params.get("beginDate"), params.get("endDate"));
        return lqw;
    }

    /**
     * 新增清扫保洁
     */
    @Override
    public Boolean insertByBo(CleaningBo bo) {
        Cleaning add = BeanUtil.toBean(bo, Cleaning.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改清扫保洁
     */
    @Override
    public Boolean updateByBo(CleaningBo bo) {
        Cleaning update = BeanUtil.toBean(bo, Cleaning.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Cleaning entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除清扫保洁
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

	@Override
	public Boolean batchInsert(List<CleaningImportDetailVo> voList) {
		if (null != voList && !voList.isEmpty()) {
			Date date = voList.get(0).getDate();
			date.setHours(0);
			date.setMinutes(0);
			date.setSeconds(0);
			LambdaQueryWrapper<Cleaning> lqw = Wrappers.lambdaQuery();
			lqw.eq(Cleaning::getDate, date);
			this.baseMapper.delete(lqw);
		}

		List<Cleaning> list = BeanCopyUtils.copyList(voList, Cleaning.class);
		list.forEach(e -> {
			e.setId(null);
			e.restBase();
		});
		return this.baseMapper.insertBatch(list);
	}

    @Override
    public List<CleaningPicVo> getAfterPicByDate(String date) {
        ArrayList<CleaningPicVo> list = new ArrayList<>();
        List<CleaningPicBo> cleaningPicBos = baseMapper.selectPicByDate(date, StatusConstant.FINISHED);
        cleaningPicBos.forEach(e->{
            if (StringUtils.isNotBlank(e.getPic2())) {
                CleaningPicVo cleaningPicVo = new CleaningPicVo();
                cleaningPicVo.setPic(e.getPic2());
                list.add(cleaningPicVo);
            }
        });
        return list;
    }

    @Override
    public List<CleaningPicVo> getBeforePicByDate(String date) {
        ArrayList<CleaningPicVo> list = new ArrayList<>();
        List<CleaningPicBo> cleaningPicBos = baseMapper.selectPicByDate(date, StatusConstant.FINISHED);
        cleaningPicBos.forEach(e->{
            if (StringUtils.isNotBlank(e.getPic1())) {
                CleaningPicVo cleaningPicVo = new CleaningPicVo();
                cleaningPicVo.setPic(e.getPic1());
                list.add(cleaningPicVo);
            }
        });
        return list;
    }

    @Override
    public Double getSumByDate(String date) {
        return baseMapper.selectSumByDate(date,null, StatusConstant.FINISHED);
    }

    @Override
    public Double getSumByDateAndCommunity(String date, String community) {
        return baseMapper.selectSumByDate(date,community, StatusConstant.FINISHED);
    }

    @Override
    public List<CleaningVo> queryListByDate(String date, String community) {
        LambdaQueryWrapper<Cleaning> lqw = Wrappers.lambdaQuery();
        lqw.eq(Cleaning::getDate,date);
        lqw.orderByDesc(Cleaning::getArea);
        if (StringUtils.isNotEmpty(community)){
            lqw.eq(Cleaning::getAreaName,community);
        }
        List<CleaningVo> cleaningVos = baseMapper.selectVoList(lqw);
        cleaningVos.forEach(e->{
            String parseDateToStr = DateUtils.parseDateToStr("yyyy-MM-dd", e.getDate());
            e.setDate(DateUtils.parseDate(parseDateToStr));
            String serviceType = e.getServiceType().toString();
            if (StringUtils.isNotEmpty(serviceType)){
                e.setServiceTypeName(ServiceTypeEnum.getByValue(serviceType));
            }
        });
        return cleaningVos;
    }

}
